package login;

import javax.persistence.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import bean.Message;

@WebServlet(name = "Upload",urlPatterns =  {"Upload"})
@MultipartConfig(location = "C:\\Users\\Administrator\\IdeaProjects\\project1\\src\\main\\webapp\\images")
public class Upload extends HttpServlet {

    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    //更新头像
    protected void UploadImg(Message message)
    {
        EntityManager em = getEntityManager();
        EntityTransaction tx = em.getTransaction();
        Message messageDTO=null;
        boolean flag=false;
        Query query = em.createQuery("select mes from Message mes where mes.name = :name");
        query.setParameter("name",message.getname());
        List list = query.getResultList();
        if (list.size() != 0) {
            for (Object i : list) {
                messageDTO = (Message) i;
                if (messageDTO.getname().equals(message.getname())) {
                    messageDTO.setimg(message.getimg());
                    try {
                        tx.begin();
                        em.persist(messageDTO);
                        tx.commit();
                    }catch (Exception e)
                    {
                        tx.rollback();
                        e.printStackTrace();
                    }finally {
                        em.close();
                    }
                }
            }
        }

    }
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取请求项，封装成Part对象
        // request.getPart方法的传参为前端页面from表单的input文件组件的name值
        Part part = request.getPart("picture");
        if(part==null){
            System.out.println(part);
            return;
        }

        // part.getSubmittedFileName()方法获取上传文件的文件名
        String fileName = part.getSubmittedFileName();
        System.out.println("上传文件的文件名: " + fileName);

        // 根据@MultipartConfig的location配置的目录上传文件，直接传参文件名即可。
        part.write(fileName);

        response.getWriter().write("文件上传成功！");


        Message messageDTO=(Message)request.getSession().getAttribute("LocalUser");
        messageDTO.setimg(fileName);
        request.getSession().setAttribute("LocalUser",messageDTO);
        UploadImg(messageDTO);
        response.sendRedirect("userinfo.jsp");

    }
}
